package com.atguigu.yygh.result;

import com.atguigu.yygh.enums.ResultEnum;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author A
 * @version 1.0
 * @description:     封装统一的返回结果
 * @date 2025/3/6 1:46
 */
@Data
public class R {
    private Integer code;
    private String message;
    private Boolean success;
    private Map<String,Object> data=new HashMap<>();
    // 私有构造方法
    private R(){

    }
    // 公共静态方法
    public static R ok(){
        R r = new R();
        r.setCode(ResultEnum.SUCCESS.getCode());
        r.setMessage(ResultEnum.SUCCESS.getMessage());
        r.setSuccess(true);
        return r;
    }
    public static R error(){
        R r = new R();
        r.setCode(ResultEnum.ERROR.getCode());
        r.setMessage(ResultEnum.ERROR.getMessage());
        r.setSuccess(false);
        return r;
    }
    // 链式调用
    public R data(Map<String,Object> data){
        this.data=data;
        return this;
    }
    public R data(String key,Object value){
        this.data.put(key,value);
        return this;
    }

    public R message(String message){
        this.message=message;
        return this;
    }

    public R code(Integer code){
        this.code=code;
        return this;
    }
}
